#include "tabbar.h"

#include <string.h>
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include <assert.h>

#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlmemory.h>

#include "stack.h"
#include "script.h"

typedef struct _TARBAR_IMAGE
{
    UFO_Texture sel, unSel;
}TARBAR_IMAGE;

typedef struct _TABBAR
{
	int currentPage;
    UFO_Stack image;
    char *onSelItemChanged;
}TABBAR;

int ufoTabBarGetSelectedItem(UFO_View view)
{
    TABBAR *pTabbar = ufoViewGetUserData(view);
	if (pTabbar) {
        return pTabbar->currentPage;
    }
    
    return 0;
}

int ufoTabBarSetSelectedItem(UFO_View view, int nItem)
{
    TABBAR *pTabbar = ufoViewGetUserData(view);
	if (pTabbar) {
        int i = 0;
        UFO_View subView = ufoViewGetChild(view);
        assert(ufoStackGetSize(pTabbar->image) > nItem);
        
        while (subView) {
            TARBAR_IMAGE* image = ufoStackGetElement(pTabbar->image, i);
            
            if (nItem == i) {
                ufoViewSetBackground(subView, image->sel);
            }
            else {
                ufoViewSetBackground(subView, image->unSel);
            }
            subView = ufoViewGetNextView(subView);
            ++i;
        }
		
		pTabbar->currentPage = nItem;
        
        if (pTabbar->onSelItemChanged) {
            ufoScriptRun(view, pTabbar->onSelItemChanged, 0, 0);
        }
        
        return 1;
    }
	return 0;
}

int ufoTabBarSetSelImage(UFO_View view, int nItem, UFO_Texture tex)
{
 	TABBAR *pTabbar = ufoViewGetUserData(view);
	if (pTabbar) {
        ufoStackReSize(pTabbar->image, nItem+1);
        {
			TARBAR_IMAGE* image = ufoStackGetElement(pTabbar->image, nItem);
			image->sel = tex;
		}
        return 1;
    }
	return 0;
}

int ufoTabBarSetUnSelImage(UFO_View view, int nItem, UFO_Texture tex)
{
 	TABBAR *pTabbar = ufoViewGetUserData(view);
	if (pTabbar) {
        ufoStackReSize(pTabbar->image, nItem+1);
        {
			TARBAR_IMAGE* image = ufoStackGetElement(pTabbar->image, nItem);
			image->unSel = tex;
		}
        return 1;
    }
	return 0;
}

int internal_tabbar_adjust(UFO_View view)
{
	TABBAR *pTabbar = ufoViewGetUserData(view);
	if (pTabbar) {
		int nCount = ufoViewGetChildCount(view);
		if (nCount > 0) {
			int i = 0;
			float width = 0;
			ufoRect rect = {0};
			UFO_View subView = ufoViewGetChild(view);
			ufoViewGetRect(view, &rect);
			width = (rect.right-rect.left)/nCount;
			for (i = 0; i < nCount; ++i) {
				ufoRect subRect = {i*width, 0, i*width+width, rect.bottom-rect.top};
				ufoViewSetRect(subView, &subRect);
				subView = ufoViewGetNextView(subView);
			}
		}
		return 1;
	}
	return 0;
}

int default_tabbar_proc(UFO_View view, int id, void* wParam, void* lParam)
{
	TABBAR *pTabbar = ufoViewGetUserData(view);
	if (!pTabbar) {
		pTabbar = malloc(sizeof(TABBAR));
		memset(pTabbar, 0, sizeof(TABBAR));
		ufoViewSetUserData(view, pTabbar);
	}

	switch (id)
	{
	case MSG_SIZE_CHANGE:
		{
			internal_tabbar_adjust(view);
		}
		break;

	case MSG_TOUCH_DOWN:
		{
            int nCount = ufoViewGetChildCount(view);
            if (nCount > 0) {
                ufoPoint *pt = wParam;
                int nItem = 0;
                float width = 0;
                ufoRect rect = {0};
                ufoViewGetRect(view, &rect);
                width = (rect.right-rect.left)/nCount;
                nItem = pt->x/width;
                ufoTabBarSetSelectedItem(view, nItem);
            }
		}
		break;

	case MSG_ADD_CHILDVIEW:
		{
			internal_tabbar_adjust(view);
		}
		break;
            
    case MSG_XML:
        {
            xmlNodePtr node = wParam;
            //### deal event
            node = node->children;
            while (node) {
                if (xmlStrcmp(node->name, (const xmlChar*)"OnSelectItemChanged") == 0 &&
                    !pTabbar->onSelItemChanged) {
                    pTabbar->onSelItemChanged = xmlNodeGetContent(node);
                    break;
                }
                node = node->next;
            }
        }
        break;
            
    case MSG_CREATE:
        {
            pTabbar->image = ufoStackCreate(sizeof(TARBAR_IMAGE));
        }
        break;
	}
	return ufoViewDefaultProc(view, id, wParam, lParam);
}